package com.shooter.dao;

import com.shooter.utils.MathUtil;
import com.shooter.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SQLWhere {
    private static final int DESC = 1;
    private String where;   //查询条件
    private int page;       //查询页数
    private int pageSize;   //分页大小
    private Map<String, String> order = new HashMap<>();//排序字段
    private List<String> grouo = new ArrayList<>();//不重复字段数据
    private String order_type = " ";//排序 默认升序
    private Map<String, String> in = new HashMap<>();
    private Map<String, String> wq = new HashMap<>();

    /**
     * 添加排序字段  默认升序
     *
     * @param desc true降序  false升序
     */
    public SQLWhere addOrder(String key, boolean desc) {
        order.put(key, order_type = desc ? " DESC " : " ");
        return this;
    }

    //默认不传升序
    public void addOrder(String key) {
        addOrder(key, false);
    }

    //默认id排序
    public void addOrder(boolean desc) {
        addOrder("id", desc);
    }

    //添加不重复数据查询字段
    public void addGrouo(String key) {
        grouo.add(key);
    }


    //获取查询条件
    public String getWhere() {
        String ww = "";
        if (where == null || where.length() == 0) {
            ww = "";
        } else {
            ww = " WHERE " + where;
        }
        String i = toWhere(in, "in");
        String w = toWhere(wq, null);
        if (i.length() != 0 || w.length() != 0)
            ww = (ww.length() == 0 ? " WHERE " : ww + (i.length() != 0 ? " AND " : "")) + i + w;
        return ww;
    }

    //统计方式
    public String getGrouo() {
        String o = "";
        if (grouo.size() != 0) {
            o += " GROUP BY ";
            for (int j = 0; j < grouo.size(); j++) {
                if (j != 0) {
                    o += ",";
                }
                o += grouo.get(j);
            }
        }
        return o;
    }

    //获取排序方式
    public String getOrder() {
        String od = "";
        for (String key : order.keySet()) {
            String o = "";
            if (od.length() == 0) o += " ORDER BY ";
            o += key;
            o += order.get(key);
            if (od.length() != 0) od += ",";
            od += o;
        }
        return od;
    }

    //格式化查询条件为String
    private String toWhere(Map<String, String> wm, String model) {
        String w = "";
        model = model == null ? " " : model;
        for (String key : wm.keySet()) {
            if (w.length() != 0) w += " AND ";
            w += " " + key + " " + model + "(" + wm.get(key) + ")";
        }
        return w;
    }

    //获取分页条件
    public String getPageWhere() {
        if (page == 0 && pageSize == 0) return "";
        pageSize = (pageSize <= 0 ? 10 : pageSize);
        page = page < 1 ? 1 : page;
        return " LIMIT " + ((page - 1) * pageSize) + "," + pageSize + " ";
    }


    //添加查询条件
    public SQLWhere addWhere(String key, String value) {
        return addWhere(key, value, "=");
    }

    public SQLWhere addWhere(String key, String value, String decide) {
        addWhere(key, value, decide, "AND");
        return this;
    }

    /**
     * 添加查询条件
     *
     * @param key
     * @param value
     * @param decide = / !=
     * @param where  AMD / OR
     * @return
     */
    public SQLWhere addWhere(String key, String value, String decide, String where) {
        if (!StringUtil.checkStringNoNull(value)) return this;
        String d = "'";
        if (DBManager.isSqlWhere(value)) {
            d = " ";
        }
        if (StringUtil.checkStringNoNull(this.where)) {
            this.where += " " + where + " ";
        } else {
            this.where = "";
        }
        this.where += key + " " + decide + " " + d + value + d;
        return this;
    }

    //添加模糊查询
    public SQLWhere addWhereLike(String key, String value) {
        if (!StringUtil.checkStringNoNull(value)) return this;
        String d = "'";
        if (!(this.where == null || this.where.equals(""))) {
            this.where += " AND ";
        } else {
            where = "";
        }
        this.where += key + " like '%" + value + "%'";
        return this;
    }

    /**
     * 链表查询
     * 多项允许where 多条查询
     *
     * @param key      key条件名
     * @param whereSql 链表的sql查询语句
     */
    public SQLWhere addWhereIn(String key, String whereSql) {
        in.put(key, whereSql);
        return this;
    }

    /**
     * 链表查询
     *
     * @param key      key条件名
     * @param whereSql 链表的sql查询语句
     */
    public SQLWhere addWhereSql(String key, String whereSql) {
        wq.put(key, whereSql);
        return this;
    }

    //设置分页
    public SQLWhere setPageWhere(int page, int pageSize) {
        return setPageWhere("" + page, "" + pageSize);
    }

    //设置分页
    public SQLWhere setPageWhere(String page, String pageSize) {
        this.page = MathUtil.isNumeric(page) ? Integer.valueOf(page) : 0;
        this.pageSize = MathUtil.isNumeric(pageSize) ? Integer.valueOf(pageSize) : 0;
        return this;
    }

    //获取分页
    public int getPage() {
        return page == 0 ? 1 : page;
    }

    //获取分页大小
    public int getPageSize() {
        if (pageSize <= 0) return 20;
        return pageSize;
    }

    //清除所有条件
    public void clear() {
        where = null;
        in.clear();
        wq.clear();
        page = 0;
        pageSize = 0;
    }
}
